package com.zw.april14thh.config;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class ShrioConfig {

    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){
        final DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
        // url需要登录后才可以访问
//        definition.addPathDefinition("/system/*","authc");
        // url无需登录即可访问
        definition.addPathDefinition("/system/login","anon");
        definition.addPathDefinition("/system/*","authc");

        return definition;
    }

    @Bean
    public Realm customRealm(){
        CUstomRealm cUstomRealm = new CUstomRealm();

        //设置密码 的处理方式
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();

        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        hashedCredentialsMatcher.setHashIterations(1024);

        cUstomRealm.setCredentialsMatcher(hashedCredentialsMatcher);

        return cUstomRealm;
    }


    @Bean
    public CookieRememberMeManager rememberMeManager(){
        CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
        SimpleCookie rememberMeCookie = new SimpleCookie("rememberMe");
        rememberMeCookie.setMaxAge(28800);
        rememberMeManager.setCookie(rememberMeCookie);
        rememberMeManager.setCipherKey(Base64.decode("8AVMNDmUM1M=aPrTzksnJQ=="));
        return rememberMeManager;
    }
}